package com.dajun.request;

import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class HolidayRequest {

    //flowable 简单流程测试
    public static void main(String[] args) {
        //1.配置数据库连接
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://localhost:3306/text?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai")
                .setJdbcUsername("root")
                .setJdbcPassword("123456")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver") // 明确设置 MySQL 驱动
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        //2.创建流程引擎
        ProcessEngine processEngine = cfg.buildProcessEngine();

        //部署流程文件
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                //根据传递的申请类型选择拼接流程文件
                .addClasspathResource("processes/leave.bpmn20.xml")
                .deploy();
//        //查询实例
//        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
//                .deploymentId(deploy.getId())
//                .singleResult();

//        System.out.println("Found process definition : " + processDefinition.getName());
        System.out.println("ProcessEngine 创建成功: " + processEngine);

        //数据封装
        Scanner scanner= new Scanner(System.in);


        RuntimeService runtimeService = processEngine.getRuntimeService();

        //封装流程变量
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("user", "大俊");
        variables.put("days", 5);
        variables.put("description", "six");

        //启动流程实例
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("leave", variables);

        //任务查寻与审批
        //查询待 managers处理的任务
        TaskService taskService = processEngine.getTaskService();
        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
        System.out.println("你有 " + tasks.size() + " 条待审批任务:");
        //展示任务详情
        for (int i=0; i<tasks.size(); i++) {
            System.out.println((i+1) + ") " + tasks.get(i).getName());
            runtimeService.deleteProcessInstance(tasks.get(i).getName(),"手动终止");
        }

        System.out.println("处理那条数据?");

        //选择任务并审批
        int taskIndex = Integer.valueOf(scanner.nextLine());
        Task task = tasks.get(taskIndex - 1);

        String processInstanceId = task.getProcessInstanceId();

        Map<String, Object> processVariables = taskService.getVariables(task.getId());
        System.out.println(processVariables.get("employee") + " 想请 " +
                processVariables.get("nrOfHolidays") + "天假，是否通过?");

        //做出审批判断，通过还是驳回
        boolean approved = scanner.nextLine().toLowerCase().equals("y");
        variables = new HashMap<String, Object>();
        variables.put("approved", approved);

        taskService.complete(task.getId(), variables);
    }
}